-- ==
-- tags { autodiff }
-- entry: vecmin
-- input { [5i64, 3i64, 2i64, 4i64, 3i64, 3i64, 4i64, 2i64, 2i64, 3i64]
--                  [[8i32, 5i32, -2i32, 4i32, 6i32],
--                   [12i32, 8i32, 7i32, 2i32, 6i32],
--                   [3i32, 9i32, -2i32, 11i32, 1i32],
--                   [7i32, 3i32, 12i32, 7i32, 10i32],
--                   [9i32, 12i32, 4i32, 1i32, 8i32],
--                   [7i32, -1i32, 11i32, 6i32, 10i32],
--                   [-2i32, 6i32, 7i32, 1i32, 12i32],
--                   [8i32, 0i32, 9i32, 6i32, 7i32],
--                   [7i32, 3i32, 6i32, 7i32, 8i32],
--                   [1i32, 4i32, 2i32, 9i32, 9i32]]
--                  [[2i32, 2i32, 2i32, 2i32, 4i32],
--                   [2i32, 1i32, 3i32, 3i32, 1i32],
--                   [2i32, 5i32, 2i32, 4i32, 5i32],
--                   [1i32, 2i32, 1i32, 1i32, 4i32],
--                   [5i32, 1i32, 4i32, 4i32, 5i32],
--                   [4i32, 1i32, 4i32, 5i32, 3i32]] }
-- output { [[4i32, 1i32, 4i32, 5i32, 3i32],
--           [0i32, 0i32, 0i32, 0i32, 4i32],
--           [2i32, 0i32, 2i32, 0i32, 5i32],
--           [0i32, 1i32, 0i32, 0i32, 5i32],
--           [0i32, 0i32, 0i32, 1i32, 0i32],
--           [0i32, 2i32, 0i32, 0i32, 0i32],
--           [5i32, 0i32, 4i32, 4i32, 0i32],
--           [0i32, 5i32, 0i32, 4i32, 0i32],
--           [0i32, 0i32, 0i32, 0i32, 0i32],
--           [1i32, 0i32, 1i32, 0i32, 0i32]] }

entry vecmin [n] [d] [bins] (is: [n]i64) (vs: [n][d]i32) (adj_out: [bins][d]i32) =
  vjp (hist (map2 i32.min) (replicate d i32.highest) bins is) vs adj_out
